<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('消息通知列表')"/>
</head>
<body>
<div class="layuimini-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="col-sm-12 search-collapse">
            <form id="formId" class=" layui-form layui-form-pane">
                <div class="layui-form-item">

                    <div class="layui-inline">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-inline">
                            <input type="text" name="title" placeholder="请输入标题"
                                class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">内容</label>
                        <div class="layui-input-inline">
                            <input type="text" name="content" placeholder="请输入内容"
                                class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">发送人</label>
                        <div class="layui-input-inline">
                            <input type="text" name="sender" placeholder="请输入发送人"
                                class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">接收者</label>
                        <div class="layui-input-inline">
                            <input type="text" name="accept" placeholder="请输入接收者"
                                class="layui-input" />
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-md" lay-submit lay-filter="notice-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="layui-btn layui-btn-md layui-btn-primary">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="notice-table" lay-filter="notice-table"></table>
        <script type="text/html" id="notice-toolbar">
            <button sec:authorize="hasPermission('/sys/notice/add','sys:notice:add')"
                    class="layui-btn layui-btn-primary layui-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <button sec:authorize="hasPermission('/sys/notice/remove','sys:notice:del')"
                    class="layui-btn layui-btn-danger layui-btn-md" lay-event="batchRemove">
                <i class="layui-icon layui-icon-delete"></i>
                删除
            </button>
        </script>
        <script type="text/html" id="notice-bar">
            <button sec:authorize="hasPermission('/sys/notice/edit','sys:notice:edit')"
                    class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>
            </button>
            <button sec:authorize="hasPermission('/sys/notice/remove','sys:notice:del')"
                    class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i
                    class="layui-icon layui-icon-delete"></i>
            </button>
        </script>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script>
    let table;
    layui.use(['table', 'form', 'jquery', 'dictionary'], function () {
        table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let prefix = pName + "/system/notice/";

        let cols = [
            [
                {type: 'checkbox'},
                        {
                            field: 'id',
                            title: '编号'
                        },
                        {
                            field: 'title',
                            title: '标题'
                        },
                        {
                            field: 'content',
                            title: '内容'
                        },
                        {
                            field: 'sender',
                            title: '发送人'
                        },
                        {
                            field: 'accept',
                            title: '接收者'
                        },
                        {
                            field: 'type',
                            title: '类型'
                        },
                        {
                            field: 'updateId',
                            title: '修改人'
                        },
                        {
                            field: 'remark',
                            title: '备注'
                        },
                {title: '操作', toolbar: '#notice-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#notice-table',
            url: prefix + 'data',
            page: true,
            escape: true,
            cols: cols,
            skin: 'line',
            toolbar: '#notice-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('checkbox(notice-table)', function(obj){

        });
        table.on('row(notice-table)', function (obj) {

        });
        table.on('tool(notice-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(notice-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(notice-query)', function (data) {
            table.reload('notice-table', {where: data.field})
            return false;
        });

        window.add = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '新增消息通知',
                shade: 0.1,
                maxmin: true,
                area: ['99%', '99%'],
                content: prefix + 'add'
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.edit = function (obj) {
            let layerIndex = layer.open({
                type: 2,
                title: '修改消息通知',
                shade: 0.1,
                maxmin: true,
                area: ['99%', '99%'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该消息通知', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些消息通知', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('notice-table');
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('notice-table', {where: param});
        }
    })
</script>
</body>
</html>